package logic

import (
	"context"
	deptApi "mldong/api/dept"
	dao "mldong/internal/modules/sys/dao"
	"mldong/internal/modules/sys/entity"
	service "mldong/internal/modules/sys/service"
	"mldong/utility"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/jinzhu/copier"
)

func init() {
	// 注册服务实现
	utility.Register(service.DeptServiceType, NewDeptLogic())
}

type DeptLogic struct {
	deptDao *dao.DeptDao
}

// 保存部门接口实现
func (logic *DeptLogic) Save(req *deptApi.SaveReq) (res bool, err error) {
	var dept = entity.Dept{}
	err = g.DB().Transaction(context.Background(), func(ctx context.Context, tx gdb.TX) error {
		req.ID = ""
		// 将req对象赋值给dept
		copier.Copy(&dept, &req)
		// 插入sys_dept表
		dept, err = logic.deptDao.Insert(dept, tx)
		if err != nil {
			return err
		}
		return nil
	})
	return dept.ID != "", err
}

// 修改部门接口实现
func (logic *DeptLogic) Update(req *deptApi.UpdateReq) (res bool, err error) {
	var dept = entity.Dept{}
	// 将req对象赋值给dept
	copier.Copy(&dept, &req)
	var count int64
	count, err = logic.deptDao.UpdateById(dept, nil)
	if err != nil {
		return false, err
	}
	return count > 0, err
}

// 删除部门接口实现
func (logic *DeptLogic) Remove(ids []string) (res bool, _ error) {
	// 遍历ids
	// for _, id := range ids {
	// 	logic.deptDao.DeleteById(id)
	// }
	var count, err = logic.deptDao.DeleteByIds(ids)
	if err != nil {
		return false, err
	}
	if count == 0 {
		return false, err
	}
	return true, err
}

// 根据ID获取部门信息
func (logic *DeptLogic) Detail(id string) (res deptApi.DeptVO, err error) {
	var deptVO = deptApi.DeptVO{}
	var dept entity.Dept
	dept, err = logic.deptDao.GetById(id)

	// 将dept对象赋值给deptVO
	copier.Copy(&deptVO, &dept)

	return deptVO, err
}

// 分页查询部门接口实现
func (logic *DeptLogic) Page(req *deptApi.PageReq) (res deptApi.DeptPageVO, err error) {
	// 处理分页参数nil值
	utility.HandlePageParam(&req.PageParam)
	depts, total, err := logic.deptDao.Page(*req.PageNum, *req.PageSize, func(model *gdb.Model) *gdb.Model {
		return utility.BuildQuery(req.PageParam, model)
	})
	var deptVOS = []deptApi.DeptVO{}
	copier.Copy(&deptVOS, &depts)
	deptPage := deptApi.DeptPageVO{
		Rows: deptVOS,
	}
	deptPage.PageNum = *req.PageNum
	deptPage.PageSize = *req.PageSize
	deptPage.RecordCount = total
	// 计算 TotalPage
	if *req.PageSize > 0 {
		deptPage.TotalPage = (total + *req.PageSize - 1) / *req.PageSize
	} else {
		deptPage.TotalPage = 0 // 如果 PageSize 为 0，则 TotalPage 为 0
	}
	return deptPage, err
}

// 查询部门列表接口实现
func (logic *DeptLogic) List(req *deptApi.ListReq) (res []deptApi.DeptVO, err error) {
	pageNum := 1
	pageSize := 10000
	req.PageNum = &pageNum
	req.PageSize = &pageSize
	pageReq := deptApi.PageReq{}
	copier.Copy(&pageReq, &req)
	ress, err := logic.Page(&pageReq)
	if err != nil {
		return nil, err
	}
	return ress.Rows, err
}

// 查询部门树接口实现
func (logic *DeptLogic) Tree(req *deptApi.TreeReq) (res []deptApi.DeptVO, err error) {
	listReq := deptApi.ListReq{}
	copier.Copy(&listReq, &req)
	res, err = logic.List(&listReq)
	if err != nil {
		return nil, err
	}
	// 转换为指针列表
	nodes := make([]*deptApi.DeptVO, len(res))
	for i := range res {
		nodes[i] = &res[i]
	}

	// 使用泛型 TreeTool
	treeTool := &utility.TreeTool[*deptApi.DeptVO]{}
	treeNodes := treeTool.ListToTree(nodes, "0")

	// 将指针列表转换回值列表
	result := make([]deptApi.DeptVO, len(treeNodes))
	for i, node := range treeNodes {
		result[i] = *node
	}
	return result, nil
}
func NewDeptLogic() service.IDeptService {
	return &DeptLogic{
		deptDao: dao.NewDeptDao(),
	}
}
